package com.qba.trafficbb.persistencia;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

import android.content.Context;

import com.google.gson.Gson;
import com.qba.trafficbb.modelo.Senal;





public class LectorSenales {
	
	
	public static Senal[] leeSenales(int archivo,Context ctx)
    {
        Senal[] result = null;
        
        try{
            Gson gson=new Gson();
            String json=readStream(ctx.getResources().openRawResource(archivo));
            json=json.substring(0, json.length()-1);
            result = gson.fromJson(json, Senal[].class);     
        }
        catch(Exception ex)
        {
            ex.printStackTrace();    
        }
        
        return result;
    }
    
	public static String readStream(InputStream is) {
        StringBuilder sb = new StringBuilder(512);
        
        
        try {
            Reader r = new InputStreamReader(is,Charset.forName("UTF-8"));
            int c = 0;
            while (c != -1) {
                c = r.read();
                sb.append((char) c);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return sb.toString();
    }
	
    
}
